import os

def generateData():
	for ff in files:
		if ff.count('.txt'):
			rank = open(path + ff)
			for line in rank:
			    if line.split('\t'):
					l = line.split('\t')
					if l[0].strip().lower() == 'rank':
						value_name =l[2].strip().lower().replace(' ','_')
					if l[0].strip().isdigit():
						if l[2].strip('$ ').replace(',','').isdigit():
							type = '1'
						else:
							type = '2'
						break
			file = open(path + type + '/' + ff,"w")
			for line in open(path + ff):
			    if line.split('\t'):
					l = line.split('\t')
					if l[0].strip().isdigit():
						country = l[1].strip().lower().replace(' ','_').replace(',','')
						value = l[2].strip('$ ').replace(',','')
						date = l[3].strip()
						print country + ','+ value_name +',' + value + ',' + type #+ ',' + date
						file.write(country + ','+ value_name +','+ value + ',' + type +'\n')
			file.close()
		
def generateList(filename):
	file = open(path + 'list/' + filename,"w")
	for line in open(path + filename):
		if line.split('\t'):
			l = line.split('\t')
			if l[0].strip().isdigit():
				country = l[1].strip().lower().replace(' ','_').replace(',','')
				print country 
				file.write(country + ',')
	file.close()

path = 'C://labs/Daatum/svn/data/'
files = os.listdir(path)
generateList('population.txt')